﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.zhoubian
{
    public partial class zhoubian_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.zhoubian().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                GetCategoryID();
                GetQuanziID();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定类别=================================
        private void GetCategoryID()
        {
            BLL.zhoubian_category bll = new BLL.zhoubian_category();
            DataTable dt = bll.GetList(0, "","sort_id asc,id asc").Tables[0];

            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("请选择类别...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();

                this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定圈子=================================
        private void GetQuanziID()
        {
            BLL.quanzi bll = new BLL.quanzi();
            DataTable dt = bll.GetList(0, "", "sort_id asc, id desc").Tables[0];

            this.ddlQuanzi.Items.Clear();
            this.ddlQuanzi.Items.Add(new ListItem("请选择所属圈子...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.ddlQuanzi.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.zhoubian bll = new BLL.zhoubian();
            Model.zhoubian model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            txtTitle.Text = model.title.ToString();
            ddlCategoryId.SelectedValue = model.category_id.ToString();
            ddlQuanzi.SelectedValue = model.quanzi_id.ToString();
            txtRemark.Text = model.remark;
            txtLianxiren.Text = model.lianxiren;
            txtMobile.Text = model.mobile;
            //不是相册图片就绑定
            string filename = model.img_url.Substring(model.img_url.LastIndexOf("/") + 1);
            //绑定图片相册
            if (filename.StartsWith("thumb_"))
            {
                hidFocusPhoto.Value = model.img_url; //封面图片
            }
            //绑定图片相册
            rptAlbumList.DataSource = model.albums;
            rptAlbumList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.zhoubian model = new Model.zhoubian();
            BLL.zhoubian bll = new BLL.zhoubian();

            model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
            model.title = txtTitle.Text.Trim();
            model.remark = txtRemark.Text.Trim();
            model.user_id = 0;
            model.quanzi_id = Utils.StrToInt(ddlQuanzi.SelectedValue, 0);
            model.lianxiren = txtLianxiren.Text.Trim();
            model.mobile = txtMobile.Text.Trim();
            model.add_time = DateTime.Now;
            model.hits = 0;
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);

            #region 保存相册====================
            model.img_url = hidFocusPhoto.Value;
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
            if (albumArr != null && albumArr.Length > 0)
            {
                List<Model.common_albums> ls = new List<Model.common_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    if (imgArr.Length == 3)
                    {
                        if (!string.IsNullOrEmpty(remarkArr[i]))
                        {
                            ls.Add(new Model.common_albums { channel_id = 4, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                        }
                        else
                        {
                            ls.Add(new Model.common_albums { channel_id = 4, original_path = imgArr[1], thumb_path = imgArr[2] });
                        }
                    }
                }
                model.albums = ls;
            }
            #endregion
            
            if (bll.Add(model) > 0)
            {
                return true;
            }

            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.zhoubian bll = new BLL.zhoubian();
            Model.zhoubian model = bll.GetModel(_id);

            model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
            model.title = txtTitle.Text.Trim();
            model.remark = txtRemark.Text.Trim();
            model.quanzi_id = Utils.StrToInt(ddlQuanzi.SelectedValue, 0);
            model.lianxiren = txtLianxiren.Text.Trim();
            model.mobile = txtMobile.Text.Trim();
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);

            #region 保存相册====================
            //检查是否有自定义图片
            model.img_url = hidFocusPhoto.Value;
            if (model.albums != null)
            {
                model.albums.Clear();
            }
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
            if (albumArr != null)
            {
                List<Model.common_albums> ls = new List<Model.common_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    int img_id = Utils.StrToInt(imgArr[0], 0);
                    if (imgArr.Length == 3)
                    {
                        if (!string.IsNullOrEmpty(remarkArr[i]))
                        {
                            ls.Add(new Model.common_albums { id = img_id, channel_id = 4, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                        }
                        else
                        {
                            ls.Add(new Model.common_albums { id = img_id, channel_id = 4, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] });
                        }
                    }
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Update(model))
            {
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改信息成功！", "zhoubian_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加信息成功！", "zhoubian_list.aspx", "parent.loadMenuTree");
            }
        }
    }
}